import sys, os
from Ui_main import *
from PyQt4 import *
from PyQt4.QtGui import *
from PyQt4.QtCore import *
from PyQt4.Qwt5.qplt import *
from Numeric import *
from numpy import *
from scipy import *
from pylab import *
import matplotlib
from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg
from numpy.fft import fft, rfft, fftshift, fftfreq
import def_algo as algor

TempAdres = 'D:/1'

class Magnet(QtGui.QMainWindow):
    def __init__(self):
        QtGui.QMainWindow.__init__(self)
        self.ui = Ui_mainWin()
        self.ui.setupUi(self)
        
        
        self.connect(self.ui.btnK1, QtCore.SIGNAL("clicked()"),  self.OtvorSuborK1)
        self.connect(self.ui.btnK2, QtCore.SIGNAL("clicked()"),  self.OtvorSuborK2)
        self.connect(self.ui.btnK3, QtCore.SIGNAL("clicked()"),  self.OtvorSuborK3)
        self.connect(self.ui.btnK4, QtCore.SIGNAL("clicked()"),  self.OtvorSuborK4)
        
        self.connect(self.ui.pushButton,  QtCore.SIGNAL("clicked()"), self.Kresli)
        
        self.ui.tabMag.setColumnCount(4)
        
        self.ui.tabMag.setHorizontalHeaderItem(0, QTableWidgetItem('K1'))
        self.ui.tabMag.setHorizontalHeaderItem(1, QTableWidgetItem('K2'))
        self.ui.tabMag.setHorizontalHeaderItem(2, QTableWidgetItem('K3'))
        self.ui.tabMag.setHorizontalHeaderItem(3, QTableWidgetItem('K4'))
        
        self.k1=[]
        self.k2=[]
        self.k3=[]
        self.k4=[]

        
    def OtvorSuborK1(self):
        row = 0
        i=0
        subor = QtGui.QFileDialog.getOpenFileName(self, 'Choose a file to open', '/home', 'Text Files *.txt')
        if subor:
            self.ui.lineK1.setText(subor)
            f = open(subor, 'r')
            self.k1 = loadtxt(f)
        
            for line in self.k1:
                self.ui.qPlot.clear()
                self.ui.qPlot_fft.clear()
                if self.ui.tabMag.rowCount() < row:
                    self.ui.tabMag.insertRow(row)
                elif self.ui.tabMag.rowCount() == row:
                    self.ui.tabMag.insertRow(row)
                self.ui.tabMag.setItem(row, 0, QTableWidgetItem(str(line)))
           
                row = row + 1
           
            self.grid = QwtPlotGrid()
            self.grid.enableXMin(True)
            self.grid.setMajPen(QPen(Qt.white, 0, Qt.DotLine))
            self.grid.setMinPen(QPen(Qt.gray, 0 , Qt.DotLine))
            self.grid.attach(self.ui.qPlot)
        
            self.curve = QwtPlotCurve("K1")
            self.curve2 = QwtPlotCurve("FFT")
                
            ff = rfft(self.k1)
        
            self.curve.setPen(QPen(Qt.red))
            self.curve2.setPen(QPen(Qt.blue))
            r = arange(0,  len(self.k1), 1)
            r2 = arange(0,  len(ff), 1)
            
            self.curve.setData(r, self.k1)
            self.curve2.setData(r2, abs(ff[1:len(ff)]))
           
            self.ui.qPlot.insertLegend(Qwt.QwtLegend(), Qwt.QwtPlot.BottomLegend)
        
            self.curve.attach(self.ui.qPlot)
            self.curve2.attach(self.ui.qPlot_fft)
        
            self.ui.qPlot.replot()   
            self.ui.qPlot_fft.replot()
            
           
            
            f.close()
   
    
    def OtvorSuborK2(self):
        row = 0
        subor = QtGui.QFileDialog.getOpenFileName(self, 'Choose a file to open', '/home', 'Text Files *.txt')
        if subor:
            self.ui.qPlot_2.clear()
            self.ui.qPlot_fft_2.clear()
            self.ui.lineK2.setText(subor)
            f = open(subor, 'r')
            self.k2 = loadtxt(f)
        
            for line in self.k2:
                if self.ui.tabMag.rowCount() < row:
                    self.ui.tabMag.insertRow(row)
                elif self.ui.tabMag.rowCount() == row:
                    self.ui.tabMag.insertRow(row)
                self.ui.tabMag.setItem(row, 1, QTableWidgetItem(str(line)))
           
                row = row + 1
           
            self.grid = QwtPlotGrid()
            self.grid.enableXMin(True)
            self.grid.setMajPen(QPen(Qt.white, 0, Qt.DotLine))
            self.grid.setMinPen(QPen(Qt.gray, 0 , Qt.DotLine))
            self.grid.attach(self.ui.qPlot_2)
        
            self.curve = QwtPlotCurve("K2")
            self.curve2 = QwtPlotCurve("FFT")
        
            ff = rfft(self.k2)
            self.curve.setPen(QPen(Qt.red))
            self.curve2.setPen(QPen(Qt.blue))
            r = arange(0,  len(self.k2), 1)
            r2 = arange(0,  len(ff), 1)
            self.curve2.setData(r, abs(ff[1:len(ff)]))
            self.curve2.attach(self.ui.qPlot_fft_2)
            self.ui.qPlot_fft_2.replot()
        
            self.curve.setPen(QPen(Qt.red))
            self.curve.setData(r, self.k2)
           
            self.ui.qPlot_2.insertLegend(Qwt.QwtLegend(), Qwt.QwtPlot.BottomLegend)
        
            self.curve.attach(self.ui.qPlot_2)
        
            self.ui.qPlot_2.replot()   
            
            f.close()
        
    def OtvorSuborK3(self):
        row = 0
        subor = QtGui.QFileDialog.getOpenFileName(self, 'Choose a file to open', '/home', 'Text Files *.txt')
        if subor:
            self.ui.qPlot_3.clear()
            self.ui.qPlot_fft_3.clear()
            self.ui.lineK3.setText(subor)
            f = open(subor, 'r')
            self.k3 = loadtxt(f)
        
            for line in self.k3:
                if self.ui.tabMag.rowCount() < row:
                    self.ui.tabMag.insertRow(row)
                elif self.ui.tabMag.rowCount() == row:
                    self.ui.tabMag.insertRow(row)
                self.ui.tabMag.setItem(row, 2, QTableWidgetItem(str(line)))
           
                row = row + 1
           
            self.grid = QwtPlotGrid()
            self.grid.enableXMin(True)
            self.grid.setMajPen(QPen(Qt.white, 0, Qt.DotLine))
            self.grid.setMinPen(QPen(Qt.gray, 0 , Qt.DotLine))
            self.grid.attach(self.ui.qPlot_3)
        
            self.curve = QwtPlotCurve("K3")
            self.curve2 = QwtPlotCurve("FFT")

            ff = rfft(self.k3)
        
            self.curve.setPen(QPen(Qt.red))
            self.curve2.setPen(QPen(Qt.blue))
            r = arange(0,  len(self.k3), 1)
            r2 = arange(0,  len(ff), 1)
            self.curve.setData(r, self.k3)
            self.curve2.setData(r2, abs(ff[1:len(ff)]))
            self.curve2.attach(self.ui.qPlot_fft_3)
            self.ui.qPlot_fft_3.replot()
        
            self.ui.qPlot_3.insertLegend(Qwt.QwtLegend(), Qwt.QwtPlot.BottomLegend)
        
            self.curve.attach(self.ui.qPlot_3)
        
            self.ui.qPlot_3.replot()   
            
            f.close()
    
    def OtvorSuborK4(self):
        row = 0
        subor = QtGui.QFileDialog.getOpenFileName(self, 'Choose a file to open', '/home', 'Text Files *.txt')
        if subor:
            self.ui.qPlot_4.clear()
            self.ui.qPlot_fft_4.clear()
            self.ui.lineK4.setText(subor)
            f = open(subor, 'r')
            self.k4 = loadtxt(f)
        
            for line in self.k4:
                if self.ui.tabMag.rowCount() < row:
                    self.ui.tabMag.insertRow(row)
                elif self.ui.tabMag.rowCount() == row:
                    self.ui.tabMag.insertRow(row)
                self.ui.tabMag.setItem(row, 3, QTableWidgetItem(str(line)))
           
                row = row + 1
           
            self.grid = QwtPlotGrid()
            self.grid.enableXMin(True)
            self.grid.setMajPen(QPen(Qt.white, 0, Qt.DotLine))
            self.grid.setMinPen(QPen(Qt.gray, 0 , Qt.DotLine))
            self.grid.attach(self.ui.qPlot_4)
        
            self.curve = QwtPlotCurve("K4")
            self.curve2 = QwtPlotCurve("FFT")

            ff = rfft(self.k4)
            self.curve.setPen(QPen(Qt.red))
            self.curve2.setPen(QPen(Qt.blue))
            r2 = arange(0,  len(ff), 1)
            self.curve2.setData(r2, abs(ff[1:len(ff)]))
            self.curve2.attach(self.ui.qPlot_fft_4)
            self.ui.qPlot_fft_4.replot()
        
        
            self.curve.setPen(QPen(Qt.red))
            r = arange(0,  len(self.k4), 1)
        
            self.curve.setData(r, self.k4)
           
            self.ui.qPlot_4.insertLegend(Qwt.QwtLegend(), Qwt.QwtPlot.BottomLegend)
        
            self.curve.attach(self.ui.qPlot_4)
        
            self.ui.qPlot_4.replot()   
            
            f.close()

    
    def Kresli(self):
        B = algor.Algo(10, 10, self.k1, self.k2, self.k3, self.k4)
        dat = B.Vypocet()
        for i in range(0, 10):
            obr = resize(dat[:,i], (10, 10))
            
            
            im = self.ui.plotWidget.canvas.ax.imshow(obr,  interpolation='bicubic', animated=True)
            im.set_data(obr)
        show()
    
